/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clases;
 import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import java.io.File;
import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import org.jfree.data.time.Day;
import org.jfree.data.time.*;
/**
 *
 * @author Ele
 */
public class GraficoCrecimiento {

 public void Conectar(){
        try{
            mdbc = new conexion();
            mdbc.init();
            Connection conn= mdbc.getMyConnection();
            stmt = conn.createStatement();
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Problemas al concetarse a la Base de Datos","Error",JOptionPane.ERROR_MESSAGE);
        }
    }  
 
 
public void GenerarGrafico(int idSenasa){    
    
    try{
    String consulta= "SELECT dl.peso,YEAR(l.fecha) as año ,MONTH(l.fecha) as mes , DAY(l.fecha) as dia from detalle_lectura dl, bovino b, lectura l where b.idSenasa=dl.idsenasa and b.idSenasa='"+idSenasa+"' and dl.idlectura=l.idlectura order by l.fecha";
    Conectar();
    
    ResultSet rs= stmt.executeQuery(consulta);
    rs.beforeFirst();
    org.jfree.data.time.TimeSeries pop = new org.jfree.data.time.TimeSeries("Linea de Crecimiento Peso", Day.class);

    while(rs.next()){
    float peso= rs.getFloat("peso");
    int d = Integer.valueOf(rs.getString("dia"));
    int m = Integer.valueOf(rs.getString("mes"));
    int a = Integer.valueOf(rs.getString("año"));
    
pop.add(new Day(d, m, a),peso);
    }
/*pop.add(new Day(2, 2, 2007), 150);
pop.add(new Day(2, 3, 2007), 200);
pop.add(new Day(2, 4, 2007), 250);
pop.add(new Day(2, 5, 2007), 300);
pop.add(new Day(2, 6, 2007), 1500);*/
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(pop);
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Crecimiento Peso de animal N°: "+idSenasa,
"Fecha",
"Peso",
dataset,
true,
true,
false);
try {
  
ChartUtilities.saveChartAsJPEG(new File("D:/Graficos/GraficoPeso2.jpg"), chart, 500, 300);
} catch (IOException e) {
System.err.println("Error creando grafico.");
}

}catch(Exception ex){
    System.err.println("Error SQL EN CONSULTA PARA GRÁFICO");
}
}
    
  private conexion mdbc;
  private Statement stmt;   
  
}